﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using Manager.App_Code.DAL;
using System.Data;

namespace Manager.App_Code.DAL
{
    class TienNghiDAL:StandardConnectDB,ITienNghi
    {
        public List<DTO.TienNghiDTO> GetListTienNghi()
        {
            List<TienNghiDTO> listTienNghi = new List<TienNghiDTO>();
            DataTable dt = DTExecuteSelect("Select * from TienNghi");
            foreach (DataRow dr in dt.Rows)
            {
                listTienNghi.Add(ConvertObjectDataRowToTienNghiDTODataRow(dr));
            }
            return listTienNghi;
            
        }

        public DTO.TienNghiDTO GetTienNghiByID(string MaTN)
        {
            TienNghiDTO tiennghi = new TienNghiDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTN", MaTN);
            DataTable dt = DTExecuteSelect("SP_TienNghi_GetByID", paramlist);
            tiennghi = ConvertObjectDataRowToTienNghiDTODataRow(dt.Rows[0]);
            return tiennghi;
            
        }

        public bool InsertTienNghi(DTO.TienNghiDTO TienNghi)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTN", TienNghi.MaTienNghi);
            paramlist.Add("@TenTN", TienNghi.TenTienNghi);
            return ExecuteUpdate("SP_TienNghi_Insert", paramlist);
        }

        public bool DeleteTienNghi(DTO.TienNghiDTO TienNghi)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTN", TienNghi.MaTienNghi);
            return ExecuteUpdate("SP_TienNghi_Delete", paramlist);
            
        }

        public bool UpdateTienNghi(DTO.TienNghiDTO TienNghi)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTN", TienNghi.MaTienNghi);
            paramlist.Add("@TenTN", TienNghi.TenTienNghi);
            return ExecuteUpdate("SP_TienNghi_Update", paramlist);
            
        }


        public TienNghiDTO ConvertObjectDataRowToTienNghiDTODataRow(DataRow dr)
        {
            if (dr != null)
            {
                TienNghiDTO tiennghi = new TienNghiDTO();
                tiennghi.MaTienNghi = dr["MaTN"].ToString();
                tiennghi.TenTienNghi = dr["TenTN"].ToString();
                return tiennghi;
            }
            else
                return null;
        }
    }
}
